package com.yoogor.xposedtest;

import android.os.Bundle;

import com.yoogor.xposedtest.hook.MMHook;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * @author caipengrui 2018/5/28
 *         Package com.yoogor.xposedtest
 *         Title:  Main
 *         Description: (用一句话描述该文件做什么)
 *         Copyright (c) 2018 优菓科技有限公司 All rights reserved.
 *         Create DateTime: 2018/5/28.
 */
public class Main implements IXposedHookLoadPackage {

    private String urlSuffix = "";
    private List<MMMessageEntity> mmMessageEntities = new ArrayList<>();
    private Bundle _bundle = new Bundle();
    private Object jjz;
    private AtomicInteger atomicInteger = new AtomicInteger();


    private Runnable broadcastReceiver = new Runnable() {

        public void run() {
            WebsocketConn.sendMessage("继续加载：" + mmMessageEntities.size());
            for (int i = 0; i < 50; i++) {
                if (!mmMessageEntities.isEmpty()) {
                    MMMessageEntity mmMessageEntity = mmMessageEntities.remove(0);
                    final Bundle bundle1 = new Bundle();
                    bundle1.putAll(_bundle);
                    bundle1.putString("geta8key_data_req_url", mmMessageEntity.getUrl());
                    Bundle bundle2 = new Bundle();
                    bundle2.putInt("scene_end_type", 233);
                    bundle2.putInt("scene_end_listener_hash_code", _bundle.getInt("webview_binder_id"));
                    XposedHelpers.callMethod(jjz, "a", 5, bundle2, _bundle.getInt("webview_binder_id"));
                    XposedHelpers.callMethod(jjz, "s", 233, bundle1);
                }
            }

        }
    };

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        MMHook.processName = lpparam.processName;
        WebsocketConn.instance();
//        WebsocketConn.sendMessage("进入Hook测试");
        if (lpparam.packageName.equals("com.yoogor.demo")) {
            Class TestActivity = XposedHelpers.findClass("com.yoogor.demo.feature.TestActivity", lpparam.classLoader);
            XposedHelpers.findAndHookMethod(TestActivity, "onCreate", Bundle.class, new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                }

                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    Object object = XposedHelpers.getObjectField(param.thisObject, "btnTrain");
                    Object label = XposedHelpers.callMethod(object, "getLabel");
                    XposedHelpers.callMethod(label, "setText", "123");
                }
            });
        } else if (lpparam.packageName.equals("com.tencent.mm")) {
            MMHook.init(lpparam);
//            Class activityBaseClass = XposedHelpers.findClass("android.app.Activity", lpparam.classLoader);
//            Class webviewActivity = XposedHelpers.findClass("com.tencent.mm.plugin.webview.ui.tools.WebViewUI", lpparam.classLoader);
//            final Class mmWeviewClass = XposedHelpers.findClass("com.tencent.mm.ui.widget.MMWebView", lpparam.classLoader);
//            //            XposedHelpers.findAndHookMethod(webviewActivity, "onCreate", Bundle.class, new XC_MethodHook() {
////                @Override
////                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
////                    super.afterHookedMethod(param);
////                    Object intent = XposedHelpers.callMethod(param.thisObject, "getIntent");
////                    Bundle bundle = (Bundle) XposedHelpers.callMethod(intent, "getExtras");
////                    for (String key : bundle.keySet()) {
////                        XposedBridge.log("key:" + key + ",val:" + bundle.get(key));
////                    }
////                }
////            });
//            XposedHelpers.findAndHookConstructor("com.tencent.mm.modelsimple.g", lpparam.classLoader, String.class, String.class, int.class, int.class, byte[].class, new XC_MethodHook() {
//                @Override
//                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                    super.beforeHookedMethod(param);
//                    WebsocketConn.sendMessage("new g:" + String.format("%s,%s,%d,%d", param.args[0], param.args[1], param.args[2], param.args[3]));
//                }
//            });
//            XposedHelpers.findAndHookConstructor("com.tencent.mm.openim.b.e", lpparam.classLoader, String.class, new XC_MethodHook() {
//                @Override
//                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                    super.beforeHookedMethod(param);
//                    WebsocketConn.sendMessage("new e:" + String.format("%s", param.args[0]));
//                }
//            });
////            XposedHelpers.findAndHookConstructor("com.tencent.mm.modelsimple.g", lpparam.classLoader, String.class, int.class, int.class, int.class, String.class, int.class, byte[].class, new XC_MethodHook() {
////                @Override
////                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
////                    super.beforeHookedMethod(param);
////                    WebsocketConn.sendMessage("new g:" + String.format("%s,%d,%d,%d,%s,%d", param.args[0], param.args[1], param.args[2], param.args[3], param.args[4], param.args[5]));
////                }
////            });
//            XposedHelpers.findAndHookMethod(activityBaseClass, "onCreate", Bundle.class, new XC_MethodHook() {
//                @Override
//                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                    WebsocketConn.sendMessage("in onCreate:" + param.thisObject.getClass().toString());
//                    Intent intent = (Intent) XposedHelpers.callMethod(param.thisObject, "getIntent");
//                    if (intent != null) {
//                        WebsocketConn.sendMessage("bundle:" + intent.toURI());
//                    }
//                    super.beforeHookedMethod(param);
//                }
//            });
//            XposedHelpers.findAndHookMethod(activityBaseClass, "onResume", new XC_MethodHook() {
//                @Override
//                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                    WebsocketConn.sendMessage("in onResume:" + param.thisObject.getClass().toString());
////                    Intent intent = (Intent) XposedHelpers.callMethod(param.thisObject, "getIntent");
////                    if (intent != null) {
////                        WebsocketConn.sendMessage("bundle:" + intent.toURI());
////                    }
//                    super.beforeHookedMethod(param);
//                }
//            });
//            XposedHelpers.findAndHookMethod(activityBaseClass, "onPause", new XC_MethodHook() {
//                @Override
//                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                    WebsocketConn.sendMessage("in onPause:" + param.thisObject.getClass().toString());
////                    Intent intent = (Intent) XposedHelpers.callMethod(param.thisObject, "getIntent");
////                    if (intent != null) {
////                        WebsocketConn.sendMessage("bundle:" + intent.toURI());
////                    }
//                    super.beforeHookedMethod(param);
//                }
//            });
//            Class stubClass = XposedHelpers.findClass("com.tencent.mm.plugin.scanner.util.e", lpparam.classLoader);
//            XposedHelpers.findAndHookMethod(stubClass, "a", Activity.class, int.class, String.class, boolean.class, new XC_MethodHook() {
//                @Override
//                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                    super.beforeHookedMethod(param);
//                    WebsocketConn.sendMessage("search for result:" + String.format("%s,%d,%s,%s", param.args[0].getClass().toString(), param.args[1], param.args[2], String.valueOf(param.args[3])));
//                }
//            });
//            XposedHelpers.findAndHookMethod(stubClass, "a", int.class, int.class, String.class, XposedHelpers.findClass("com.tencent.mm.ac.l", lpparam.classLoader), new XC_MethodHook() {
//                @Override
//                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                    super.afterHookedMethod(param);
//                    Object object = param.args[param.args.length - 1];
//                    if (233 == (int) XposedHelpers.callMethod(object, "getType")) {
//                        WebsocketConn.sendMessage("QE:" + XposedHelpers.callMethod(object, "QE") + "\tQI():" + XposedHelpers.callMethod(object, "QI"));
////                        if (atomicInteger.incrementAndGet() >= 50) {
////                            atomicInteger.set(0);
////                            WebsocketConn.sendMessage("发送广播");
////                            WebsocketConn.sendMessage("completed");
////                        }
//                    } else if (106 == (int) XposedHelpers.callMethod(object, "getType")) {
//                        WebsocketConn.sendMessage("type:" + (int) XposedHelpers.callMethod(object, "getType"));
//                    } else {
//                        WebsocketConn.sendMessage("type:" + (int) XposedHelpers.callMethod(object, "getType"));
//                    }
////                    if (object.getClass().toString().equals("com.tencent.mm.ac.l")) {
////
////                    } else {
////                        WebsocketConn.sendMessage("not type com.tencent.mm.ac.l:" + object.getClass().toString());
////                    }
//                }
//            });
//            Class stubServiceClass = XposedHelpers.findClass("com.tencent.mm.plugin.webview.stub.WebViewStubService", lpparam.classLoader);
//            XposedHelpers.findAndHookMethod(stubServiceClass, "a", int.class, int.class, String.class, XposedHelpers.findClass("com.tencent.mm.ac.l", lpparam.classLoader), new XC_MethodHook() {
//                @Override
//                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                    super.afterHookedMethod(param);
//                    Object object = param.args[param.args.length - 1];
//                    if (233 == (int) XposedHelpers.callMethod(object, "getType")) {
//                        WebsocketConn.sendMessage("QE:" + XposedHelpers.callMethod(object, "QE") + "\tQI():" + XposedHelpers.callMethod(object, "QI"));
////                        if (atomicInteger.incrementAndGet() >= 50) {
////                            atomicInteger.set(0);
////                            WebsocketConn.sendMessage("发送广播");
////                            WebsocketConn.sendMessage("completed");
////                        }
//                    } else if (106 == (int) XposedHelpers.callMethod(object, "getType")) {
//                        WebsocketConn.sendMessage("type:" + (int) XposedHelpers.callMethod(object, "getType"));
//                    } else {
//                        WebsocketConn.sendMessage("type:" + (int) XposedHelpers.callMethod(object, "getType"));
//                    }
////                    if (object.getClass().toString().equals("com.tencent.mm.ac.l")) {
////
////                    } else {
////                        WebsocketConn.sendMessage("not type com.tencent.mm.ac.l:" + object.getClass().toString());
////                    }
//                }
//            });
//            Class stubInterfaceClass = XposedHelpers.findClass("com.tencent.mm.plugin.webview.stub.d$a$a", lpparam.classLoader);
//            Class stubServiceClass1 = XposedHelpers.findClass("com.tencent.mm.plugin.webview.stub.WebViewStubService$1", lpparam.classLoader);
//            XposedHelpers.findAndHookMethod(stubServiceClass1, "s", int.class, Bundle.class, new XC_MethodHook() {
//                @Override
//                protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
//                    super.afterHookedMethod(param);
//                    if (233 == (int) param.args[0]) {
//                        Bundle bundle = (Bundle) param.args[1];
//                        _bundle.putAll(bundle);
//                        Intent intent = new Intent();
//                        if (bundle != null) {
//                            intent.putExtras(bundle);
//                        }
////                        WebsocketConn.sendMessage("stubServiceClass bundle:" + intent.toURI());
//
//                    }
//                }
//            });
//            XposedHelpers.findAndHookMethod(webviewActivity, "Bo", int.class, new XC_MethodHook() {
//                @Override
//                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//                    super.afterHookedMethod(param);
////                    WebsocketConn.sendMessage("webviewActivity Bo:" + param.args[0]);
//                    jjz = XposedHelpers.getObjectField(param.thisObject, "jjz");
//                    WebsocketConn.setOnReloadListener(broadcastReceiver);
//                    if ((int) param.args[0] == 233 && mmMessageEntities != null && !mmMessageEntities.isEmpty() && _bundle != null) {
////                        while (!mmMessageEntities.isEmpty()) {
////                            MMMessageEntity mmMessageEntity = mmMessageEntities.remove(0);
////                            final Bundle bundle1 = new Bundle();
////                            bundle1.putAll(_bundle);
////                            bundle1.putString("geta8key_data_req_url", mmMessageEntity.getUrl());
////                            Bundle bundle2 = new Bundle();
////                            bundle2.putInt("scene_end_type", 233);
////                            bundle2.putInt("scene_end_listener_hash_code", _bundle.getInt("webview_binder_id"));
////                            XposedHelpers.callMethod(jjz, "a", 5, bundle2, _bundle.getInt("webview_binder_id"));
////                            XposedHelpers.callMethod(jjz, "s", 233, bundle1);
////                        }
//
//                        for (int i = 0; i < 50; i++) {
//                            if (!mmMessageEntities.isEmpty()) {
//                                MMMessageEntity mmMessageEntity = mmMessageEntities.remove(0);
//                                final Bundle bundle1 = new Bundle();
//                                bundle1.putAll(_bundle);
//                                bundle1.putString("geta8key_data_req_url", mmMessageEntity.getUrl());
//                                Bundle bundle2 = new Bundle();
//                                bundle2.putInt("scene_end_type", 233);
//                                bundle2.putInt("scene_end_listener_hash_code", _bundle.getInt("webview_binder_id"));
//                                XposedHelpers.callMethod(jjz, "a", 5, bundle2, _bundle.getInt("webview_binder_id"));
//                                XposedHelpers.callMethod(jjz, "s", 233, bundle1);
//                            }
//                        }
//                    }
//                }
//            });
//            XposedHelpers.findAndHookMethod(stubInterfaceClass, "s", int.class, Bundle.class, new XC_MethodHook() {
//                @Override
//                protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
//                    super.afterHookedMethod(param);
//                    if (233 == (int) param.args[0]) {
//                        Bundle bundle = (Bundle) param.args[1];
//                        Intent intent = new Intent();
//
//                        if (bundle != null) {
//                            _bundle.putAll(bundle);
//                            intent.putExtras(bundle);
//                        }
//
////                        WebsocketConn.sendMessage("bundle:" + intent.toURI());
////                        if (mmMessageEntities != null && !mmMessageEntities.isEmpty()) {
////                            for (MMMessageEntity mmMessageEntity : mmMessageEntities) {
////                                final Bundle bundle1 = new Bundle();
////                                bundle1.putAll(_bundle);
//////                            bundle1.putInt("webview_binder_id", hashCode());
////                                bundle1.putString("geta8key_data_req_url", mmMessageEntity.getUrl());
//////                        int binderId = _bundle != null ? _bundle.getInt("webview_binder_id") : 0;
////                                String url1 = mmMessageEntities.get(0).getUrl();
//////                        XposedHelpers.callMethod(jjz, "cB", url1, binderId);
//////                        bundle1.putInt("geta8key_session_id", (int) System.currentTimeMillis());
//////                                mmMessageEntities.clear();
//////                            new Thread(new Runnable() {
//////                                @Override
//////                                public void run() {
//////                                    try {
//////                                        Thread.sleep(1000);
//////                                    } catch (InterruptedException e) {
//////                                        e.printStackTrace();
//////                                    }
////                                Bundle bundle2 = new Bundle();
////                                bundle2.putInt("scene_end_type", 233);
////                                bundle2.putInt("scene_end_listener_hash_code", _bundle.getInt("webview_binder_id"));
////                                XposedHelpers.callMethod(param.thisObject, "a", 5, bundle2, _bundle.getInt("webview_binder_id"));
////                                XposedHelpers.callMethod(param.thisObject, "s", 233, bundle1);
////                            }
////                                }
////                            }).start();
////                        }
//                    }
//                }
//            });
//
        }
    }
}
